package com.weizhizs.assistant.code.template.data;

import com.weizhizs.assistant.code.config.ParamConfig;
import com.weizhizs.assistant.code.helper.BeanHelper;
import com.weizhizs.assistant.code.helper.TableHelper;
import com.weizhizs.assistant.code.template.IDataLoador;
import com.weizhizs.assistant.code.vo.TableVO;
import com.weizhizs.assistant.utils.DateUtils;
import com.weizhizs.assistant.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class JavaAppControllerData implements IDataLoador {

    public static String getClassName(String tableName) {
        return TableHelper.tableNameToClassName(tableName) + "Controller";
    }

    public static String getFullClassName(String classPath, String tableName) {
        return classPath + "." + getClassName(tableName);
    }

    @Override
    public String getTemplateName(TableVO tableVO) {
        return ParamConfig.getInstance().getTemplatePath() + "/app.controller.java.vm";
    }

    @Override
    public String getFileName(String tableName) {
        return getClassName(tableName) + ".java";
    }

    @Override
    public String getFileOutPath() {
        return ParamConfig.getInstance().getAppControllerOutPath();
    }

    @Override
    public Map<String, Object> getDataMap(TableVO tableVO) {
        String tableName = tableVO.getTableName();

        Map<String, Object> map = new HashMap<>();
        map.put("tableName", tableName);
        map.put("tableComment", tableVO.readTableComment());
        map.put("tableCommentFull", tableVO.getTableComment());
        map.put("authorName", ParamConfig.getInstance().getAuthorName());
        map.put("version", ParamConfig.getInstance().getVersion());
        map.put("currentDate", DateUtils.nowDateString());

        map.put("entityOutPath", getFileOutPath());
        map.put("entityClassName", getClassName(tableName));

        map.put("validatePath", ParamConfig.getInstance().getValidatePath());

        map.put("baseFullClassName", ParamConfig.getInstance().getAppControllerBaseClass());
        map.put("baseClassName", BeanHelper.getBeanClassName(
                ParamConfig.getInstance().getAppControllerBaseClass()));

        map.put("pk", tableVO.getPk());

        map.put("dtoClassName", JavaDtoData.getClassName(tableName));
        map.put("dtoFullClassName", JavaDtoData.getFullClassName(ParamConfig.getInstance().getDtoOutPath(), tableName));

        map.put("queryClassName", JavaQueryData.getClassName(tableName));
        map.put("queryFullClassName", JavaQueryData.getFullClassName(ParamConfig.getInstance().getQueryOutPath(), tableName));

        map.put("serviceClassName", JavaAppServiceData.getClassName(tableName));
        map.put("serviceFullClassName", JavaAppServiceData.getFullClassName(ParamConfig.getInstance().getAppServiceOutPath(), tableName));
        map.put("VOPath", ParamConfig.getInstance().getAppVOOutPath());
        map.put("serviceInstanceName", StringUtils.firstToLowerCase(JavaAppServiceData.getClassName(tableName)));

        String dtoInstanceName = StringUtils.firstToLowerCase(TableHelper.tableNameToClassName(tableName));
        String requestPath = TableHelper.getRequestPath(dtoInstanceName);
        String permPath = requestPath.replace("/", ":");

        map.put("dtoInstanceName", dtoInstanceName);
        map.put("permPath", permPath);
        map.put("requestPath", requestPath);
        map.put("urlRoot", ParamConfig.getInstance().getAppUrlRoot());

        if (tableVO.haveDisableFlagField()) {
            map.put("haveDisableFlag", 1);
            map.put("keyFieldSetMethod",
                    "set" + StringUtils.firstToUpperCase(tableVO.getPk().getAttrName()));
        } else {
            map.put("haveDisableFlag", 0);
        }

        return map;
    }

}
